<template>
<main>
  <div class="login">
    <h3>小u商城后台管理系统</h3>
        <div class="grid-content bg-purple">
          <!-- model 表单数据 -->
          <!-- rules 表单的校验规则 -->
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
          <!-- prop检验使用的字段 -->
            <el-form-item label="" prop="username" label-width='20px'>
              <el-input v-model="ruleForm.username">
                <template slot="prepend"><i class="el-icon-user"></i></template>
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="password"  label-width='20px'>
              <el-input type="password" v-model="ruleForm.password" show-password>
                <template slot="prepend"><i class="el-icon-key"></i></template>
              </el-input>
            </el-form-item>

            <el-form-item   label-width='20px'>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >立即登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
  </div>
</main>
  
</template>

<script>
export default {
  data() {
    return {
        //表单的数据向后台传值时就用这个
      ruleForm: {
        username: "",
        password:"",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        
      },
    };
  },
  methods: {
    submitForm(formName) {
        // this.$refs.ruleForm，找到子组件，调用validate方法，来校验表单
      this.$refs[formName].validate((valid) => {
        if (valid) {
            //为真通过
          this.$http.post('/userlogin',this.ruleForm)
          .then(res=>{
            //登录成功的话需要保存数据
            if(res.code===200){
              console.log(res);
              this.$store.commit('saveUserInfo', res.list)
              this.$router.push('/')
            }
          }).catch(err=>{
            console.log(err);
          })
        } else {
            //为假不通过
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
};
</script>
<style scoped>
main{
  height: 100vh;
  width: 100vw;
  background-image: url('../assets/微信图片_20211207090434.jpg');
  background-repeat: no-repeat;
  overflow: hidden;
}
main h3{
  text-align: center;
  margin-top: 30px;
  color: rgb(251,109,73);
}
.login{
  margin: 200px auto;
  width: 40%;
  background-color: white;
  overflow: hidden;
  padding-right: 20px;
  height: 300px;
  border-radius: 5px;
  box-shadow: 0 0 8px 1px #333;
}
.login .grid-content {
  height: 400px;
}
/* 覆盖elementui样式 */
.login .el-button{
    width: 100%;
    
}
.el-form{
  margin: 50px auto;
}
</style>